﻿using BackData;
using Domain.RBAC;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Users;

namespace Team.API.Applocation.Handler.Users
{
    public class DeleteUserHandler : IRequestHandler<DeleteUserCommand, APIResult<int>>
    {
        private readonly IRepository<Usermodel> userRepository;

        public DeleteUserHandler(IRepository<Usermodel> userRepository)
        {
            this.userRepository = userRepository;
        }

        public async Task<APIResult<int>> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var user = await userRepository.GetByIdAsync(request.Id);
            if (user == null || user.IsDel)
            {
                return APIResult<int>.Faile("用户不存在");
            }

            user.IsDel = true;
            var rows = await userRepository.UpdateAsync(user);
            if (rows <= 0)
            {
                return APIResult<int>.Faile("删除用户失败");
            }

            return APIResult<int>.Ok("删除用户成功", rows);
        }
    }
}
